package com.yeswater.rabbitmq_spingboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Author : yeswater
 */

@Configuration
public class TTLQueueConfig {

    // 普通交换机
    public static final String EXCHANGE_X = "X";

    // 死信交换机
    public static final String DEAD_LETTER_EXCHANGE_Y = "Y";

    // 普通队列
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    public static final String QUEUE_C = "QC";

    // 死信队列
    public static final String DEAD_LETTER_QUEUE_D = "QD";

    @Bean("exchangeX")
    public static DirectExchange exchangeX(){
        return new DirectExchange(EXCHANGE_X);
    }

    @Bean("exchangeY")
    public static DirectExchange exchangeY(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE_Y);
    }

    @Bean("queueA")
    public static Queue queueA(){
        Map<String,Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_Y);
        arguments.put("x-dead-letter-routing-key", "YD");
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    @Bean("queueB")
    public static Queue queueB(){
        Map<String,Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_Y);
        arguments.put("x-dead-letter-routing-key", "YD");
        arguments.put("x-message-ttl", 20000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    /**
     * 优化延迟队列 不添加 ttl,但是这种方法有很大缺陷
     */
    @Bean("queueC")
    public static Queue queueC(){
        Map<String,Object> arguments = new HashMap<>(2);
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_Y);
        arguments.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }



    @Bean("queueD")
    public static Queue queueD(){
        Map<String,Object> arguments = new HashMap<>();
        return QueueBuilder.durable(DEAD_LETTER_QUEUE_D).withArguments(arguments).build();
    }

    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("exchangeX") DirectExchange exchangeX) {
        return BindingBuilder.bind(queueA).to(exchangeX).with("XA");
    }

    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("exchangeX") DirectExchange exchangeX) {
        return BindingBuilder.bind(queueB).to(exchangeX).with("XB");
    }

    /**
     * 优化延迟队列,但是这种方法有很大缺陷
     */
    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("exchangeX") DirectExchange exchangeX) {
        return BindingBuilder.bind(queueC).to(exchangeX).with("XC");
    }

    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("exchangeY") DirectExchange exchangeY) {
        return BindingBuilder.bind(queueD).to(exchangeY).with("YD");
    }

}
